package com.swift.violent.mainstay.config.expand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.alenfive.rocketapi.extend.IUserAuthorization;
import com.swift.violent.mainstay.common.constants.Global;
import com.swift.violent.mainstay.common.constants.ResultDesc;
import com.swift.violent.mainstay.common.util.SecurityUtil;
import com.swift.violent.mainstay.core.base.core.modal.po.ApiUserEntity;
import com.swift.violent.mainstay.core.base.core.service.ApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录鉴权
 * The type Default user authorization.
 *
 * @author xuhaifeng
 * @date 2021.1.19 21:48
 */
@Component
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
public class DefaultUserAuthorization implements IUserAuthorization {
    @Autowired
    private ApiUserService userService;

    @Override
    public String validate(String username, String password) {
        final QueryWrapper<ApiUserEntity> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.eq(ApiUserEntity.ACCOUNT, username);
        queryWrapperUser.eq(ApiUserEntity.ACCOUNT_STATUS, Global.NUM_1);
        final ApiUserEntity apiUserEntity = userService.getOne(queryWrapperUser, false);
        return SecurityUtil.matchesPassword(password, apiUserEntity.getSecretKey()) ? ResultDesc.OK.getDesc() : null;
    }
}